package scut.facerecognition.Utils;

import org.springframework.web.multipart.MultipartFile;
import scut.facerecognition.constant.UserConstant;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.UUID;

public class UploadFileUtils {
    public static String uploadFile(MultipartFile file, String location) {
        //设置文件存储根路径
        Path rootLocation = Paths.get(location);
        Path destination = null;
        //将图片存至本地
        try {
            //生成唯一文件名（避免重名覆盖）
            String originalFilename = file.getOriginalFilename();
            String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
            String newFilename = System.currentTimeMillis() + "_" + UUID.randomUUID() + fileExtension;

            //保存文件到本地
            destination = rootLocation.resolve(newFilename);
            Files.copy(file.getInputStream(), destination, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException e) {
            System.out.println("文件保存失败");
        }

        return  destination.toString();
    }
}
